package com.zzdx.deployment.web.controller.people;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.system.domain.*;
import com.zzdx.deployment.system.mapper.CompanyMapper;
import com.zzdx.deployment.system.mapper.PcMapper;
import com.zzdx.deployment.system.service.IFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.system.service.IPeopleService;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 返岗人员 信息操作处理
 * 
 * @author zzdx
 * @date 2020-03-08
 */
@Controller
@RequestMapping("/system/people")
public class PeopleController extends BaseController
{
    private String prefix = "system/people";
	
	@Autowired
	private IPeopleService peopleService;
	@Autowired
	private IFileService fileService;
	
	@RequiresPermissions("system:people:view")
	@GetMapping()
	public String people()
	{
	    return prefix + "/people";
	}

	@RequiresPermissions("system:peopleOne:view")
	@GetMapping("/{peopleId}")
	public String peopleOne(@PathVariable("peopleId") Integer peopleId,ModelMap mmap)
	{
		People people=peopleService.selectPeopleById(peopleId);
		mmap.put("people",people);
		return prefix + "/peopleOne";
	}

	@Autowired
	private PcMapper pcMapper;
	@Autowired
	private CompanyMapper companyMapper;

	@GetMapping("/dept/{pcId}")
	public String people(@PathVariable("pcId") Integer pcId, ModelMap mmap)
	{
		People people =  new People();
		people.setCompanyId(pcId);
		Pc pc =pcMapper.selectPcById(pcId);
		Company company= companyMapper.selectCompanyById(pc.getCompanyId());
		mmap.put("people", people);
		mmap.put("company",company);
		mmap.put("pc", pc);
		SysUser sysUser = getSysUser();
		return prefix + "/people";
	}
	/**
	 * 查询返岗人员列表
	 */
	@RequiresPermissions("system:people:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(People people)
	{
		startPage();
        List<People> list = peopleService.selectPeopleList(people);
		return getDataTable(list);
	}

	@RequiresPermissions("system:people:list")
	@PostMapping("/list/{pcId}")
	@ResponseBody
	public TableDataInfo list(@PathVariable("pcId") String pcId,People people)
	{
		people.setCompanyId(Integer.valueOf(pcId));
		startPage();
		List<People> list = peopleService.selectPeopleList(people);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出返岗人员列表
	 */
	@RequiresPermissions("system:people:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(People people)
    {
    	List<People> list = peopleService.selectPeopleList(people);
        ExcelUtil<People> util = new ExcelUtil<People>(People.class);
        return util.exportExcel(list, "people");
    }
	
	/**
	 * 新增返岗人员
	 */
	@GetMapping("/add/{pcId}")
	public String add(@PathVariable("pcId") String pcId, ModelMap mmap)
	{
		People people =  new People();
		people.setCompanyId(Integer.valueOf(pcId));
		mmap.put("people", people);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存返岗人员
	 */
	@RequiresPermissions("system:people:add")
	@Log(title = "返岗人员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@RequestParam("fileName") MultipartFile file,People people)
	{
		String loginName = ShiroUtils.getLoginName();
		ProductResource productResource = new ProductResource();
		String uuid=UUID.randomUUID().toString();
		productResource.setCreateBy(loginName);
		productResource.setResourceRelativePath(uuid);
		productResource.setCreateTime(new Date());
		String wholePath = fileService.upload(file, productResource,people);

		people.setPeopleLinkAdress(uuid);
		people.setPeopleFillTime(new Date());
		return toAjax(peopleService.insertPeople(people));
	}

	/**
	 *  修改保存返岗人员
	 */
	@RequiresPermissions("system:people:edit")
	@Log(title = "返岗人员", businessType = BusinessType.UPDATE)
	@PostMapping("/change")
	@ResponseBody
	public AjaxResult changeSave(@RequestParam("fileName")MultipartFile file,People people)
	{
		String loginName = ShiroUtils.getLoginName();
		ProductResource productResource = new ProductResource();
		String uuid=UUID.randomUUID().toString();
		productResource.setCreateBy(loginName);
		productResource.setResourceRelativePath(uuid);
		productResource.setCreateTime(new Date());
		String wholePath = fileService.upload(file, productResource,people);
		people.setPeopleLinkAdress(uuid);
		return toAjax(peopleService.updatePeople(people));
	}


	/**
	 * 修改返岗人员
	 */
	@GetMapping("/edit/{peopleId}")
	public String edit(@PathVariable("peopleId") Integer peopleId, ModelMap mmap)
	{
		People people = peopleService.selectPeopleById(peopleId);
		mmap.put("people", people);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存返岗人员
	 */
	@RequiresPermissions("system:people:edit")
	@Log(title = "返岗人员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(People people)
	{		
		return toAjax(peopleService.updatePeople(people));
	}
	
	/**
	 * 删除返岗人员
	 */
	@RequiresPermissions("system:people:remove")
	@Log(title = "返岗人员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(peopleService.deletePeopleByIds(ids));
	}

	
}
